﻿using SD.Common.PoweredByLee;
using SD.ContentSystem.AppService.Maps;
using SD.ContentSystem.Domain.Entities;
using SD.ContentSystem.Domain.IRepositories;
using SD.ContentSystem.Domain.Mediators;
using SD.ContentSystem.IAppService.DTOs.Outputs;
using SD.ContentSystem.IAppService.Interfaces;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.Global.Transaction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace SD.ContentSystem.AppService.Implements
{
    /// <summary>
    /// 相片服务契约实现
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class PhotoContract : IPhotoContract
    {
        #region # 字段及依赖注入构造器

        /// <summary>
        /// 领域服务中介者
        /// </summary>
        private readonly DomainServiceMediator _svcMediator;

        /// <summary>
        /// 仓储中介者
        /// </summary>
        private readonly RepositoryMediator _repMediator;

        /// <summary>
        /// 单元事务
        /// </summary>
        private readonly IUnitOfWorkCMS _unitOfWork;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        /// <param name="svcMediator">领域服务中介者</param>
        /// <param name="repMediator">仓储中介者</param>
        /// <param name="unitOfWork">单元事务</param>
        public PhotoContract(DomainServiceMediator svcMediator, RepositoryMediator repMediator,
            IUnitOfWorkCMS unitOfWork)
        {
            this._svcMediator = svcMediator;
            this._repMediator = repMediator;
            this._unitOfWork = unitOfWork;
        }

        #endregion


        //命令部分

        #region # 创建相册 —— void CreatePhotoAlbum(string albumNo, string albumName...
        /// <summary>
        /// 创建相册
        /// </summary>
        /// <param name="albumNo">相册编号</param>
        /// <param name="albumName">相册名称</param>
        /// <param name="author">作者</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void CreatePhotoAlbum(string albumNo, string albumName, string author, int sort, string description)
        {
            //验证
            Assert.IsFalse(this._repMediator.PhotoAlbumRep.Exists(albumNo), "编号已存在！");

            PhotoAlbum album = new PhotoAlbum(albumNo, albumName, author, sort, description);

            this._unitOfWork.RegisterAdd(album);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 修改相册 —— void UpdatePhotoAlbum(Guid photoAlbumId, string albumName...
        /// <summary>
        /// 修改相册
        /// </summary>
        /// <param name="photoAlbumId">相册Id</param>
        /// <param name="albumName">相册名称</param>
        /// <param name="author">作者</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void UpdatePhotoAlbum(Guid photoAlbumId, string albumName, string author, int sort, string description)
        {
            PhotoAlbum currentAlbum = this._unitOfWork.Resolve<PhotoAlbum>(photoAlbumId);
            currentAlbum.UpdateInfo(albumName, author, sort, description);

            this._unitOfWork.RegisterSave(currentAlbum);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 删除相册 —— void RemovePhotoAlbum(Guid photoAlbumId)
        /// <summary>
        /// 删除相册
        /// </summary>
        /// <param name="photoAlbumId">相册Id</param>
        public void RemovePhotoAlbum(Guid photoAlbumId)
        {
            PhotoAlbum currentAlbum = this._unitOfWork.Resolve<PhotoAlbum>(photoAlbumId);

            //验证
            Assert.IsFalse(this._repMediator.PhotoRep.ExistsByAlbum(currentAlbum.Number), "相册下存在相片，不可删除！");

            this._unitOfWork.RegisterPhysicsRemove<PhotoAlbum>(currentAlbum);
            this._unitOfWork.Commit();
        }
        #endregion


        #region # 创建相片 —— void CreatePhoto(string photoAlbumNo, string photoName, string imagePath...
        /// <summary>
        /// 创建相片
        /// </summary>
        /// <param name="photoAlbumNo">相册编号</param>
        /// <param name="photoName">相片名称</param>
        /// <param name="sort">排序</param>
        /// <param name="url">链接地址</param>
        /// <param name="imagePath">图片路径</param>
        /// <param name="thumbPath">缩略图路径</param>
        /// <param name="description">描述</param>
        public void CreatePhoto(string photoAlbumNo, string photoName, string imagePath, string thumbPath, string url, int sort, string description)
        {
            //验证
            Assert.IsTrue(this._repMediator.PhotoAlbumRep.Exists(photoAlbumNo), "相册不存在！");

            Photo photo = new Photo(photoAlbumNo, photoName, imagePath, thumbPath, url, sort, description);

            this._unitOfWork.RegisterAdd(photo);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 修改相片 —— void UpdatePhoto(Guid photoId, string photoName, string url...
        /// <summary>
        /// 修改相片
        /// </summary>
        /// <param name="photoId">相片Id</param>
        /// <param name="photoName">相片名称</param>
        /// <param name="sort">排序</param>
        /// <param name="url">链接地址</param>
        /// <param name="description">描述</param>
        public void UpdatePhoto(Guid photoId, string photoName, string url, int sort, string description)
        {
            Photo currentPhoto = this._unitOfWork.Resolve<Photo>(photoId);
            currentPhoto.UpdateInfo(photoName, url, sort, description);

            this._unitOfWork.RegisterSave(currentPhoto);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 删除相片 —— void RemovePhoto(Guid photoId)
        /// <summary>
        /// 删除相片
        /// </summary>
        /// <param name="photoId">相片Id</param>
        public void RemovePhoto(Guid photoId)
        {
            this._unitOfWork.RegisterPhysicsRemove<Photo>(photoId);
            this._unitOfWork.Commit();
        }
        #endregion


        //查询部分

        #region # 获取相册 —— PhotoAlbumInfo GetPhotoAlbum(Guid photoAlbumId)
        /// <summary>
        /// 获取相册
        /// </summary>
        /// <param name="photoAlbumId">相册Id</param>
        /// <returns>相册</returns>
        public PhotoAlbumInfo GetPhotoAlbum(Guid photoAlbumId)
        {
            PhotoAlbum currentAlbum = this._repMediator.PhotoAlbumRep.Single(photoAlbumId);
            PhotoAlbumInfo currentAlbumInfo = currentAlbum.ToDTO();

            return currentAlbumInfo;
        }
        #endregion

        #region # 获取相册 —— PhotoAlbumInfo GetPhotoAlbumByNo(string photoAlbumNo)
        /// <summary>
        /// 获取相册
        /// </summary>
        /// <param name="photoAlbumNo">相册编号</param>
        /// <returns>相册</returns>
        public PhotoAlbumInfo GetPhotoAlbumByNo(string photoAlbumNo)
        {
            PhotoAlbum currentAlbum = this._repMediator.PhotoAlbumRep.Single(photoAlbumNo);
            PhotoAlbumInfo currentAlbumInfo = currentAlbum.ToDTO();

            return currentAlbumInfo;
        }
        #endregion

        #region # 获取相册列表 —— IEnumerable<PhotoAlbumInfo> GetPhotoAlbums()
        /// <summary>
        /// 获取相册列表
        /// </summary>
        /// <returns>相册列表</returns>
        public IEnumerable<PhotoAlbumInfo> GetPhotoAlbums()
        {
            IEnumerable<PhotoAlbum> photoAlbums = this._repMediator.PhotoAlbumRep.FindAll().OrderBy(x => x.Sort);
            IEnumerable<PhotoAlbumInfo> photoAlbumInfos = photoAlbums.Select(x => x.ToDTO());

            return photoAlbumInfos;
        }
        #endregion

        #region # 分页获取相册列表 —— PageModel<PhotoAlbumInfo> GetPhotoAlbumsByPage(string keywords...
        /// <summary>
        /// 分页获取相册列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>相册列表</returns>
        public PageModel<PhotoAlbumInfo> GetPhotoAlbumsByPage(string keywords, int pageIndex, int pageSize)
        {
            int rowCount, pageCount;

            IEnumerable<PhotoAlbum> photoAlbums = this._repMediator.PhotoAlbumRep.FindByPage(keywords, pageIndex, pageSize, out rowCount, out pageCount);
            IEnumerable<PhotoAlbumInfo> photoAlbumInfos = photoAlbums.Select(x => x.ToDTO());

            return new PageModel<PhotoAlbumInfo>(photoAlbumInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion


        #region # 获取相片 —— PhotoInfo GetPhoto(Guid photoId)
        /// <summary>
        /// 获取相片
        /// </summary>
        /// <param name="photoId">相片Id</param>
        /// <returns>相片</returns>
        public PhotoInfo GetPhoto(Guid photoId)
        {
            Photo currentPhoto = this._repMediator.PhotoRep.Single(photoId);
            PhotoInfo currentPhotoInfo = currentPhoto.ToDTO();

            return currentPhotoInfo;
        }
        #endregion

        #region # 根据相册获取相片列表 —— IEnumerable<PhotoInfo> GetPhotosByAlbum(string photoAlbumNo)
        /// <summary>
        /// 根据相册获取相片列表
        /// </summary>
        /// <param name="photoAlbumNo">相册编号</param>
        /// <returns>相片列表</returns>
        public IEnumerable<PhotoInfo> GetPhotosByAlbum(string photoAlbumNo)
        {
            IEnumerable<Photo> photos = this._repMediator.PhotoRep.FindByAlbum(photoAlbumNo);
            IEnumerable<PhotoInfo> photoInfos = photos.Select(x => x.ToDTO());

            return photoInfos;
        }
        #endregion

        #region # 分页获取相片列表 —— PageModel<PhotoInfo> GetPhotosByPage(string keywords, string photoAlbumNo...
        /// <summary>
        /// 分页获取相片列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="photoAlbumNo">相册编号</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>相片列表</returns>
        public PageModel<PhotoInfo> GetPhotosByPage(string keywords, string photoAlbumNo, int pageIndex, int pageSize)
        {
            int rowCount, pageCount;

            IEnumerable<Photo> photos = this._repMediator.PhotoRep.FindByPage(keywords, photoAlbumNo, pageIndex, pageSize, out rowCount, out pageCount);
            IEnumerable<PhotoInfo> photoInfos = photos.Select(x => x.ToDTO());

            return new PageModel<PhotoInfo>(photoInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion
    }
}
